

import pyinotify

class EventHandler(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):  # 文件/目录创建事件
        print(f"新文件: {event.pathname}")

wm = pyinotify.WatchManager()
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wm.add_watch('/监控目录', pyinotify.IN_CREATE, rec=True)  # rec=True 递归监控子目录
notifier.loop()  # 启动事件循环